<?php
class sms_util_contactlist {
	static function getList($uid){
		$contactList = array();
		$contactList[0] = array();
		$contactList[1] = array();
		array_push($contactList[0], array('attention', '我的好友'));
		array_push($contactList[1], self::getFriendList($uid));
		$guild = self::getGuildList($uid);
		if($guild){
			$contactList[0] = array_merge($contactList[0], $guild[0]);
			$contactList[1] = array_merge($contactList[1], $guild[1]);
		}
		return $contactList;
	}
	
	static function getFriendList($uid){
		$o = Model::factory('friends_model_data');
		return $o->getFriendList($uid);
	}
	
	static function getGuildList($uid){
		$search = new MultiFetcher();
		$url = "http://gh.178.com/gu.php?u=$uid&s=".md5($uid."178gh") ;
		$search->push('guild', $url);
		$result = $search->fetch();
		if(!$result['guild']['error'] and $result['guild']['content']){
			list($domain, $name) = explode("@", $result['guild']['content']);
			$search->clean();
			$url = "http://gh.178.com/gm.php?d=$domain&s=".md5($domain."178gh");
			$search->push('members', $url);
			$result = $search->fetch();
			if(!$result['members']['error']){
				$members = unserialize($result['members']['content']);
				$ret = array();
				$ret[0] = array();
				$ret[1] = array();
				array_push($ret[0], array($domain, $name));
				array_push($ret[1], $members);
				return $ret;
			}
		}
		return null;
	}
	
	static function isAllowToSend($privacy, $reciever, $sender){
		$allow = 1;
		if(index_util_baduser::isBad($reciever, $sender)){
			return 0;
		}
		foreach($privacy as $p){
			switch ($p){
				case index_model_standardprop::SMS_FRIENDS :
					$allow = friends_util_checker::isFriend($reciever,$sender);
					break;
				case index_model_standardprop::SMS_FRIENDS2 :
					$allow = friends_util_checker::isFriendOfMyFriend($reciever, $sender);
					break;
				case index_model_standardprop::SMS_NGA :
					$allow = index_util_external::hasNgaCode($sender);
					break;
				case index_model_standardprop::SMS_GUILD :
					$allow = index_util_external::isGuildMember($reciever, $sender);
					break;
				default:
				case index_model_standardprop::SMS_EVERYONE :
					break;
			}
			if($allow) break;
		}
		return $allow;
	}
}

